package com.platform.framework.controller.rights;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.platform.common.utils.JsonUtils;
import com.platform.common.utils.PageUtils;
import com.platform.framework.controller.BaseController;
import com.platform.framework.entity.rights.FwPlServer;
import com.platform.framework.service.rights.FwPlServerService;

/**
 * 
  * 组织结构类 	
  * @author 吴姿杨[wuziyang] Tel:15811034326
  * @createDate 2017-7-7 下午5:16:55
 */
@RestController
@RequestMapping("/server")
public class FwPlServerController extends BaseController{


	
	@Autowired
	FwPlServerService fwPlServerService;
	/**
	 * 
	  * 查询所有的数据	
	  * @author 吴姿杨[wuziyang] Email:315646979@qq.com Tel:15811034326
	  * @param request
	  * @return server分页语句
	  * @throws Exception
	  * @createDate 2017-7-10 上午9:28:17
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@RequestMapping(value = "pagelist", method = { RequestMethod.POST })
	public String serverPageList(HttpServletRequest request) throws Exception {
		String searchCtx = request.getParameter("searchPhrase");//查询条件框
		Pageable pageable = null;
		try{
			pageable = PageUtils.dealminiRequest(request);
		}catch (Exception e) {
			e.printStackTrace();
		}
		Page<FwPlServer> pages= null;
	    pages= fwPlServerService.findServerPage(searchCtx,pageable);
		return JsonUtils.dealMiniPage(pages);
	}
	
	/**
	 * 
	  * 添加用户 	
	  * @author 吴姿杨[wuziyang] Email:315646979@qq.com Tel:15811034326
	  * @return
	  * @createDate 2017-7-13 上午9:36:57
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@PreAuthorize("hasAuthority('server:add')")
	@RequestMapping(value = "add", method = { RequestMethod.POST })
	public String serverSave(HttpServletRequest request){
		String data = request.getParameter("data");
		FwPlServer fwPlServer =  JSONObject.parseObject(data, FwPlServer.class);
		fwPlServer.setId(this.getUUID());
		fwPlServer.setCreateTime(new Date());
		fwPlServer.setCreateUserCode(this.getUserId());//操作用户
		fwPlServer.setCreateUserName(this.getUserCode());//操作编码
		return fwPlServerService.saveServer(fwPlServer);
	}
	
	/**
	 * 
	  * 更新用户 	
	  * @author 吴姿杨[wuziyang] Email:315646979@qq.com Tel:15811034326
	  * @return
	  * @createDate 2017-7-13 上午9:36:57
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@PreAuthorize("hasAuthority('server:update')")
	@RequestMapping(value = "update", method = { RequestMethod.POST })
	public String serverUpdate(HttpServletRequest request){
		String data = request.getParameter("data");
		FwPlServer fwPlServer =  JSONObject.parseObject(data, FwPlServer.class);
		fwPlServer.setUpdateTime(new Date());
		fwPlServer.setUpdateUserCode(this.getUserId());
		fwPlServer.setUpdateUserName(this.getUserCode());
		//fwPlServer.setIsDeleted("0");
		return fwPlServerService.updateServer(fwPlServer);
	}
	
	/**
	 * 
	  * 删除用户	
	  * @author 吴姿杨[wuziyang] Email:315646979@qq.com Tel:15811034326
	  * @return
	  * @createDate 2017-7-13 上午9:36:57
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@PreAuthorize("hasAuthority('server:delete')")
	@RequestMapping(value = "delete", method = { RequestMethod.DELETE })
	public String serverDelete(HttpServletRequest request){
		String ids = request.getParameter("ids");
		return fwPlServerService.deleteServer(ids);
	}
	
	
	/**
	 * 
	  * 系统服务器维护主页	
	  * @author 吴姿杨[wuziyang] Email:315646979@qq.com Tel:15811034326
	  * @return
	  * @createDate 2017-7-13 上午9:37:19
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@PreAuthorize("hasAuthority('server')")//有用户添加权限的才能访问    
    @RequestMapping("/index")  
    public ModelAndView serverIndex() {
    	ModelAndView mv = new ModelAndView("rights/server/index");
		return mv;
    }
	
	/**
	 * 
	  * 系统服务器添加，修改页面界面 	
	  * @author 吴姿杨[wuziyang] Email:315646979@qq.com Tel:15811034326
	  * @return
	  * @createDate 2017-7-13 上午9:37:43
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
    @RequestMapping("/edit")  
    public ModelAndView serverAdd(){
    	ModelAndView mv = new ModelAndView("rights/server/edit");
		return mv;
    }
    
    

	


}